package com.iss.okair.service.assets;

import com.iss.common.StringTools;
import com.iss.common.UuidTools;
import com.iss.okair.db.entity.AssetsCar;
import com.iss.okair.db.entity.Assetstrans;
import com.iss.okair.db.entity.Category;
import com.iss.okair.db.mapper.AssetsCarMapper;
import com.iss.okair.db.mapper.CategoryMapper;
import com.iss.okair.db.model.AssetsCarModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AssetsCarService {
    @Resource
    private AssetsCarMapper mapper;
    @Resource//自动实例化对象（java框架）

    private CategoryMapper cmapper;

    public List<AssetsCar> find(AssetsCarModel model){
        return mapper.select(model);
    }

    //保存
    //此步骤可以保存两个按钮
    public boolean save(AssetsCar assets){
        if(StringTools.isNullOrEmpty(assets.getId())) {
            //添加
            //设置id值
            assets.setId(UuidTools.buildUUID());
            //调用私有方法，生成编码
            assets.setAssetscode(this.buildAssetsCode(assets.getAssetstype()));

            //调用当前时间
            // assets.setCreatedate(new Date());

            return mapper.add(assets)>=1?true:false;
        }else{
            //修改
            return mapper.update(assets)>=1?true:false;
        }
    }

    /*构建资产编码的方法
    *编码规则：NOR类型编码-6位的顺序编码
    * 6位顺序编码：根据
    * */
    private String buildAssetsCode(int categoryId){
        //1.根据资产类型的id，查询出来资产类别的信息
        Category currentCategory = cmapper.selectById(categoryId);
        //2.根据查询结果和规则，生成编码String.format("NOR%s-%06s",资产类别编号,总数+1);
        String temp = String.format("NOR%s-%06d",currentCategory.getCode(),mapper.countALL()+1);//第二个字符串显示6位，不足6位的填充0
        //类型用d

        //3.将编码抛出
        return temp;

    }

}
